package cn.consult.sys.service.impl;

import cn.consult.sys.dao.TPlaceBuildingDao;
import cn.consult.sys.dao.TPlaceFloorDao;
import cn.consult.sys.dao.TPlaceRoomDao;
import cn.consult.sys.entity.TPlaceBuilding;
import cn.consult.sys.entity.TPlaceFloor;
import cn.consult.sys.entity.TPlaceRoom;
import cn.consult.sys.service.TPlaceBuildingService;
import cn.consult.sys.vo.SiteTree.PlaceFloorVo;
import cn.consult.sys.vo.SiteTree.PlaceRoomVo;
import cn.consult.sys.vo.SiteTree.SiteTreeVo;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * 楼栋(TPlaceBuilding)表服务实现类
 *
 * @author makejava
 * @since 2024-04-01 08:36:15
 */
@Service("tPlaceBuildingService")
public class TPlaceBuildingServiceImpl extends ServiceImpl<TPlaceBuildingDao, TPlaceBuilding> implements TPlaceBuildingService {

    @Autowired
    TPlaceBuildingDao tPlaceBuildingDao;
    @Autowired
    TPlaceFloorDao tPlaceFloorDao;
    @Autowired
    TPlaceRoomDao tPlaceRoomDao;

    @Override
    public List<SiteTreeVo> tree() {
        //拿到所有的楼栋 楼层 场地
        List<TPlaceBuilding> tPlaceBuildings = tPlaceBuildingDao.selectList(null);
        List<TPlaceFloor> tPlaceFloors = tPlaceFloorDao.selectList(null);
        List<TPlaceRoom> tPlaceRooms = tPlaceRoomDao.selectList(null);


        List<SiteTreeVo> siteTreeVos = new ArrayList<>();
        tPlaceBuildings.stream().forEach(
                siteTreeVo -> {
                    SiteTreeVo TreeVo = new SiteTreeVo();
                    TreeVo.setName(siteTreeVo.getBuildingName());
                    BeanUtils.copyProperties(siteTreeVo, TreeVo);
                    siteTreeVos.add(TreeVo);
                }
        );


        for (SiteTreeVo siteTreeVo : siteTreeVos) {
            for (TPlaceFloor tPlaceFloor : tPlaceFloors) {
                if (tPlaceFloor.getFloorBuildingid().equalsIgnoreCase(String.valueOf(siteTreeVo.getId()))) {
                    PlaceFloorVo floorVo = new PlaceFloorVo();
                    floorVo.setName(tPlaceFloor.getFloorName());
                    BeanUtils.copyProperties(tPlaceFloor, floorVo);
                    siteTreeVo.getChildren().add(floorVo);
                }
            }
            for (PlaceFloorVo child : siteTreeVo.getChildren()) {
                for (TPlaceRoom tPlaceRoom : tPlaceRooms) {
                    if (tPlaceRoom.getRoomFloorid().equalsIgnoreCase(String.valueOf(child.getId()))) {
                        PlaceRoomVo roomVo = new PlaceRoomVo();
                        roomVo.setName(tPlaceRoom.getRoomName());
                        BeanUtils.copyProperties(tPlaceRoom, roomVo);
                        child.getChildren().add(roomVo);
                    }
                }
            }
        }
        return siteTreeVos;
    }

    @Override
    public List<TPlaceBuilding> selectByBuildingNameOrType(Map<String, Object> params) {
        LambdaQueryWrapper<TPlaceBuilding> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.like(params.get("buildingName") != null && params.get("buildingName") != "", TPlaceBuilding::getBuildingName, params.get("buildingName"));
        queryWrapper.eq(params.get("buildingType") != null && params.get("buildingType") != "", TPlaceBuilding::getBuildingType, params.get("buildingType"));

        List<TPlaceBuilding> tPlaceBuildings = tPlaceBuildingDao.selectList(queryWrapper);
        if (tPlaceBuildings != null) {

            for (TPlaceBuilding tPlaceBuilding : tPlaceBuildings) {
                LambdaQueryWrapper<TPlaceFloor> floorLambdaQueryWrapper = new LambdaQueryWrapper<>();
                floorLambdaQueryWrapper.eq(TPlaceFloor::getFloorBuildingid, tPlaceBuilding.getId());
                //当前楼栋多少层数
                Integer i = tPlaceFloorDao.selectCount(floorLambdaQueryWrapper);
                tPlaceBuilding.setBuildingTiernumber(i);
                TPlaceBuilding building = new TPlaceBuilding();
                building.setId(tPlaceBuilding.getId());
                building.setBuildingTiernumber(i);
                tPlaceBuildingDao.updateById(building);
                //拿到当前楼栋下的所有层数  求出房间树
                int num = 0;
                List<TPlaceFloor> tPlaceFloors = tPlaceFloorDao.selectList(floorLambdaQueryWrapper);
                for (TPlaceFloor tPlaceFloor : tPlaceFloors) {
                    LambdaQueryWrapper<TPlaceRoom> lambdaQueryWrapper = new LambdaQueryWrapper<>();
                    lambdaQueryWrapper.eq(TPlaceRoom::getRoomFloorid, tPlaceFloor.getId());
                    //当前楼层的房间数量
                    Integer i1 = tPlaceRoomDao.selectCount(lambdaQueryWrapper);
                    num += i1;
                }
                tPlaceBuilding.setBuildingRoomnumber(num);
                tPlaceBuildingDao.updateById(tPlaceBuilding);
                num = 0;
            }
            return tPlaceBuildings;
        }
        return null;
    }

    @Override
    public boolean flagName(String name) {
        LambdaQueryWrapper<TPlaceBuilding> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(TPlaceBuilding::getBuildingName, name);
        List<TPlaceBuilding> tPlaceBuildings = tPlaceBuildingDao.selectList(lambdaQueryWrapper);
        if (tPlaceBuildings.size() > 0) {
            return false;
        }
        return true;
    }

    @Override
    public boolean updateBuliding(TPlaceBuilding tPlaceBuilding) {
        int i = tPlaceBuildingDao.updateById(tPlaceBuilding);
        if (i > 0) {
            return true;
        }
        return false;
    }

    @Override
    public boolean saveBuliding(TPlaceBuilding tPlaceBuilding) {
        tPlaceBuildingDao.insert(tPlaceBuilding);
        if (tPlaceBuilding.getBuildingTiernumber() != null) {
            if (tPlaceBuilding.getBuildingTiernumber() > 0L) {
                for (Integer i = 0; i < tPlaceBuilding.getBuildingTiernumber(); i++) {
                    TPlaceFloor tPlaceFloor = new TPlaceFloor();
                    tPlaceFloor.setFloorBuildingid(String.valueOf(tPlaceBuilding.getId()));
                    tPlaceFloor.setFloorSerialnumber(String.valueOf(i + 1) + "F");
                    tPlaceFloor.setFloorName(tPlaceBuilding.getBuildingName() + String.valueOf(i + 1) + "层");
                    tPlaceFloorDao.insert(tPlaceFloor);
                }
            }
        }
        return false;
    }

    @Override
    @Transactional
    public boolean deleteBuliding(Long bulidingId) {
        LambdaQueryWrapper<TPlaceFloor> floor = new LambdaQueryWrapper<>();
        floor.eq(TPlaceFloor::getFloorBuildingid, bulidingId);
        List<TPlaceFloor> tPlaceFloors = tPlaceFloorDao.selectList(floor);
        if (tPlaceFloors.size() > 0) {
            for (TPlaceFloor tPlaceFloor : tPlaceFloors) {
                LambdaQueryWrapper<TPlaceRoom> room = new LambdaQueryWrapper<>();
                room.eq(TPlaceRoom::getRoomFloorid, tPlaceFloor.getId());
                List<TPlaceRoom> tPlaceRooms = tPlaceRoomDao.selectList(room);
                if (tPlaceRooms.size() > 0) {
                    tPlaceRoomDao.delete(new LambdaQueryWrapper<TPlaceRoom>().eq(TPlaceRoom::getRoomFloorid, tPlaceFloor.getId()));
                }
                tPlaceFloorDao.delete(new LambdaQueryWrapper<TPlaceFloor>().eq(TPlaceFloor::getFloorBuildingid, bulidingId));
            }
        }
        if (tPlaceBuildingDao.deleteById(bulidingId) > 0) {
            return true;
        }
        return false;
    }
}

